package 课程设计报告_hb_7_8;

/**
 * @author Jiang Zhiyan
 *
 * 2021年7月8日 上午9:08:37
 *
 */
public class Account {
    private String id;
    private double balance;
    private boolean flag;

    /**
	 * @param balance
	 */
	public Account(String id,double balance) {
//		super();
		this.id=id;
		this.balance = balance;
	}

	/**
     * 	取款的方法
     */
    
//    public synchronized void drawMoney(double money) {
	public  void drawMoney(double money) {
		synchronized(this) {
			if(!flag) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(money > balance) {
	    		System.out.println("余额不足！");
	    	  return ;
	    	}
	    	double balance = this.balance;
	    	setBalance(balance-money);
	    	System.out.println("取款成功，余额为：" + this.getBalance() );
		}
    }

	public  void addMoney(double money) {
		synchronized(this) {
	    	double balance = this.balance;
	    	setBalance(balance-money);
	    	System.out.println("取款成功，余额为：" + this.getBalance() );
//	    	唤醒在同步器上等待的线程
	    	this.flag = true;   //表示已经存款
	    	this.notify();
		}
    }
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the balance
	 */
	public double getBalance() {
		return balance;
	}

	/**
	 * @param balance the balance to set
	 */
	public void setBalance(double balance) {
		this.balance = balance;
	}
}
